package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private DestinationRepository destinationRepository;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private DestinationServiceImpl destinationService;

    @Override
    public void insert(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.save(destination);

    }

    @Override
    public Destination get(String id) {
        return destinationRepository.findById(id).get();
    }

    @Override
    public List<Destination> list() {
        return destinationRepository.findAll();
    }

    @Override
    //关联目的地查看
    public List<Destination> getDestByRegionId(String rid) {
        //查询区域下目的地的id集合
        Region region = regionService.get(rid);
        List<String> ids = region.getRefIds();
        //以目的地id集合 查询目的地对象集合（jpa规范）
        return destinationRepository.findByIdIn(ids);
    }

    @Override
    //分页列表
    public Page<Destination> query(DestinationQuery qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template, pageable, query, Destination.class, qo);
    }

    @Override
    //目的地吐司
    public List<Destination> queryToasts(String destId) {
        //新建目的地集合
        List<Destination> list = new ArrayList<>();
        this.createToasts(list, destId);
        Collections.reverse(list);
        return list;
    }

    //前端目的地热门目的地
    @Override
    public List<Destination> queryDestByRegionIdForApi(String regionId) {
        List<Destination> list = new ArrayList<>();
        if ("-1".equals(regionId)) {
            list = destinationRepository.findByParentName("中国");
        } else {
            //非国内
            list = this.getDestByRegionId(regionId);
        }
        //遍历每一个目的地的子目的地
        for (Destination destination : list) {
            // Pageable pageable = PageRequest.of(0, 5, Sort.Direction.ASC, "_id");
            List<Destination> children = destinationRepository.findByParentId(destination.getId());
            //优化：目的地限制显示个数
            if (children.size() > 5) {
                children = children.subList(0, 5);
            }
            destination.setChildren(children);
        }

        return list;
    }


    private void createToasts(List<Destination> list, String destId) {
        if (!StringUtils.hasLength(destId)) {
            return;
        }
        Destination destination = this.get(destId);
        list.add(destination);

        if (StringUtils.hasLength(destination.getParentId())) {
            this.createToasts(list, destination.getParentId());
        }
    }

    @Override
    public Destination queryName(String name) {
        return destinationRepository.findByName(name);
    }
}
